package com.badshun.weapon;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badshun.box2d.Box2DWorld;
import com.badshun.player.MainPlayer;

public class Wand {
	private Sprite leftWand;
	private Sprite rightWand;
	private Sprite upWand;
	private Sprite downWand;
	
	public Sprite mainWand;

	private OrthographicCamera camera;
	private MainPlayer player;
	private Box2DWorld b2dWorld;
	public Vector2 position;
	
	private SpriteBatch batch;
	
	public ArrayList<Ball> ballList;
	
	public Wand(OrthographicCamera camera, MainPlayer player, Box2DWorld b2dWorld) {
		this.camera = camera;
		this.player = player;
		this.b2dWorld = b2dWorld;
		
		leftWand = new Sprite(new Texture(Gdx.files.internal("weapon/wand.png")));
		leftWand.setSize(leftWand.getWidth(), leftWand.getHeight());
		leftWand.setOrigin(leftWand.getWidth(), leftWand.getHeight() / 2);
		
		rightWand = new Sprite(new Texture(Gdx.files.internal("weapon/wand.png")));
		rightWand.setSize(rightWand.getWidth(), rightWand.getHeight());
		rightWand.setOrigin(0, rightWand.getHeight() / 2);
		rightWand.flip(true, false);
		
		upWand = new Sprite(new Texture(Gdx.files.internal("weapon/wand.png")));
		upWand.setSize(upWand.getWidth(), upWand.getHeight());
		upWand.setOrigin(0, upWand.getHeight() / 2);
		upWand.flip(true,  false);
		upWand.setRotation(90);
		
		downWand = new Sprite(new Texture(Gdx.files.internal("weapon/wand.png")));
		downWand.setSize(downWand.getWidth(), downWand.getHeight());
		downWand.setOrigin(downWand.getWidth(), downWand.getHeight() / 2);
		downWand.setRotation(90);
		
		batch = new SpriteBatch();
		
		ballList = new ArrayList<Ball>();
	}
	
	public void createBall(float knobPercentX, float knobPercentY) {
		Ball ball = new Ball(camera, b2dWorld, player, this, knobPercentX, knobPercentY);
		
		ballList.add(ball);
	}
	
	private void autoSetMainSword() {
		switch (player.direction) {
			case LEFT:
				mainWand = leftWand;
				mainWand.setPosition(position.x - player.keyFrame.getRegionWidth() / 2 - mainWand.getWidth() / 2,  position.y);
				
				break;
			case RIGHT:
				mainWand = rightWand;
				mainWand.setPosition(position.x + player.keyFrame.getRegionWidth() / 2, position.y);
				
				break;
			case UP:
				mainWand = upWand;
				mainWand.setPosition(position.x + player.keyFrame.getRegionWidth() / 2, position.y + player.keyFrame.getRegionWidth() / 2);
				
				break;
			case DOWN:
				mainWand = downWand;
				mainWand.setPosition(position.x - downWand.getWidth() / 2 - player.keyFrame.getRegionWidth() / 2, position.y - player.keyFrame.getRegionWidth() / 2);
				
				break;
		}
	}
	
	public void render() {
		position = player.position;
		
		autoSetMainSword();
		
		batch.begin();
		batch.setProjectionMatrix(camera.combined);
		mainWand.draw(batch);
		batch.end();
		
		
		if(ballList.size() > 0) {
			for(int i = 0; i < ballList.size(); i++) {
				ballList.get(i).render();
				
				if(ballList.get(i).disappear) {
					ballList.get(i).destroyBody();
					ballList.remove(i);
				}
			}
		}
	}
}





















